package com.didispace;

import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by anzhen on 2017-6-6.
 */
@Service("sendMailService")
public class SendMailService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private JavaMailSender mailSender;
	@Resource
	private VelocityEngine velocityEngine;

	public void sendMail() throws MessagingException {
		logger.info("sendMail");
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("754567844@qq.com");
		helper.setTo("975763794@qq.com");
		helper.setSubject("主题：电脑还钱");

		Map<String, Object> model = new HashMap<>();
		model.put("weixinPath", "http://121.42.62.62:8080/img/weixin.jpg");
		model.put("zhifubaoPath", "http://121.42.62.62:8080/img/zhifubao.jpg");
		String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "template.vm", "UTF-8", model);
		helper.setText(text, true);
		mailSender.send(mimeMessage);
	}
}
